<?php

namespace app\admin\validate;

use think\Validate;

class SuperUser extends Validate
{
  protected $rule = [
    'username'=>'require|max:50',
    'email'=>'checkEmail',
    'code'=>'require|max:4'
  ];
  protected $message = [
    'username.require'=>'用户名不能为空!',
    'username.max'=>'用户名过长',
    'nickname.require'=>'昵称不能为空!',
    'nickname.max'=>'昵称过长!',
    'code.require'=>'验证码不能为空!',
    'code.max'=>'验证码过长!'
  ];
  protected $scene = [
    'add' =>['username', 'nickname'],
    'save_email' =>['email', 'code']
  ];
  public function checkEmail($value, $rules, $data=[])
  {
    $pattern = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i';
    preg_match($pattern, $value, $isEmail);
    if (!$isEmail){
      return '非法的邮箱格式';
    }
    return true;
  }
}
